import Taro from "@tarojs/taro";
import { setGlobalData, getGlobalData } from "./globalData.js";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
export default {
    data() {
        return {
            mobile: "",
            //openId: "",
            code: '',
            hasPhone:true,
            apiBaseUrl: process.env.IOParking.API_HOST //请求头
        };
    },
    methods: {
        wx_login() {
            var that=this
            api.getUserInfo({
                scope: "all"
            }).then(resp => {
                if (resp.success) {
                    console.log("authCode is：", resp.data);
                    if(resp.data=='mock'){
                        setGlobalData("openId", 'oRPxj5TQ040-YRuv0v7lhRvMyRB0');//oRPxj5ZrsZGRuxTa-UrzEBeLuv-s 
                        setGlobalData("mobile", '13021250990');//17761643218
                        that.initData()
                        return
                    }
                    that.getInfos(resp.data)
                } else {
                    console.log("err is ", resp.message, "err code is ", resp.code);
                }
            });
        },
        getInfos(authCode) {
            var that=this
            Taro.request({
                url: that.apiBaseUrl + "/weixin/souchegetuser?authCode=" + authCode,
                data: {},
                header: {
                    "content-type": "application/json" // 默认值
                },
                method: "GET",
                success(res) {
                    console.log(res.data)
                    if (res.data.isSuccess) {
                        var userOpenId=res.data.data.openId
                        console.log("后台登陆获取获取openId:", userOpenId);
                        if(res.data.data.phoneNum!==''){
                            setGlobalData("openId", userOpenId);
                            setGlobalData("mobile", res.data.data.phoneNum);
                            that.initData()
                        }else{
                            setGlobalData("openId", userOpenId);
                            setGlobalData("mobile", '');
                            that.initData()
                        }
                        api.bury("userAuthorizationNum", {
                                      parkName: "",
                                      parkId: "",
                                      time: new Date().valueOf(),
                                      mobile: getGlobalData("mobile") ? getGlobalData("mobile") : ""
                                    });
                    }
                }
            });
        },
        onGetPhoneNumber({ detail }) {
            var that=this
            const { encryptedData, iv } = detail;
            console.log("phone打印:" + JSON.stringify(detail));
            if (encryptedData && iv) {
            that.authPhone(encryptedData, iv);
            that.hasPhone=true
            Taro.showLoading({
                  title: "授权中，请稍后",
                  mask: true
                });
            }
          },
          authPhone(encryptedData,iv) {
              var that=this
            api.register({
                    encryptedData: encryptedData,
                    iv: iv,
                    scope: "all"
                })
                .then(resp => {
                    Taro.hideLoading();
                    if (resp.success) {
                        console.log("用户注册 ", resp.data);
                        if(resp.data=='mock'){
                            setGlobalData("mobile", '17761643218');
                            return
                        }
                        that.getInfos(resp.data)
                    } else {
                        console.log("err is ", resp.msg, "err code is ", resp.code);
                    }
                })
                .catch(e => {
                    console.log(e);
                });
        },
    }
}